#include "Mod_HttpServer.h"
#include <string.h>
#include "httpServer.h"
#include "webpage.h"
#include "loopback.h"
#include "socket.h"      // 包含 getSn_SR、setSn_CR 等 socket 操作函数
#include "wizchip_conf.h" // 包含芯片配置、宏定义等

cHttpServer HttpServer;



/**
 * 函数功能：cForwardServer构造函数
 * 参    数：无
 * 返 回 值：无
 */
cHttpServer::cHttpServer()
{
    /*设置W5500结构体指针*/
    //this->pst_W5500Struct = &gst_W5500_Dev;

    
}

/**
 * 函数功能：cHttpServer初始化
 * 参    数：无
 * 返 回 值：无
 */
void cHttpServer::Init(void)
{
    /*初始化W5500*/

		// 主动关闭所有已配置的 socket ,通过socket.c操作寄存器实现,后期准备直接封装一层
    for (int i = 0; i < sizeof(socknumlist) / sizeof(socknumlist[0]); i++)
    {
        uint8_t sn = socknumlist[i];

        // 如果 socket 没有处于 SOCK_CLOSED，则断开并关闭
        uint8_t status = getSn_SR(sn);
        if (status != SOCK_CLOSED)
        {
            printf(">> Closing socket %d (status = 0x%02X)\r\n", sn, status);

            // 如果还连接着就先断开
            if (status == SOCK_ESTABLISHED || status == SOCK_CLOSE_WAIT)
            {
                setSn_CR(sn, Sn_CR_DISCON);
                while (getSn_CR(sn));  // 等命令完成
            }

            // 强制关闭
            setSn_CR(sn, Sn_CR_CLOSE);
            while (getSn_CR(sn));
            while (getSn_SR(sn) != SOCK_CLOSED);

            // 清除中断
            setSn_IR(sn, 0xFF);

            printf(">> Socket %d closed\r\n", sn);
        }
    }
	  
    //Drv_W5500_Init(this->pst_W5500Struct);
    printf("HttpServer_Init start\r\n");
		printf("HTTP Socket[%d] = %d\r\n", 0, socknumlist[0]);
    // HTTP Server ��ʼ��
    httpServer_init(TX_BUF, RX_BUF, sizeof(socknumlist) / sizeof(socknumlist[0]), socknumlist);
    {

        reg_httpServer_cbfunc(NVIC_SystemReset, NULL); // Callback: NXP MCU Reset

        reg_httpServer_webContent((uint8_t *)"index.html", (uint8_t *)index_page);

        reg_httpServer_webContent((uint8_t *)"netinfo.html", (uint8_t *)netinfo_page);
        reg_httpServer_webContent((uint8_t *)"netinfo.js", (uint8_t *)wiz550web_netinfo_js);
			
			  reg_httpServer_webContent((uint8_t *)"socketinfo.html", (uint8_t *)socketinfo_page);
        reg_httpServer_webContent((uint8_t *)"socketinfo.js", (uint8_t *)wiz550web_socketinfo_js);

        // LED����ҳ��
        reg_httpServer_webContent((uint8_t *)"dio.html", (uint8_t *)dio_page);
        reg_httpServer_webContent((uint8_t *)"dio.js",
                                  (uint8_t *)wiz550web_dio_js);

        // AJAX JavaScript ����
        reg_httpServer_webContent((uint8_t *)"ajax.js",
                                  (uint8_t *)wiz550web_ajax_js);

#ifdef _MAIN_DEBUG_
        display_reg_webContent_list();
#endif
    }
}


/**
 * 函数功能：处理网口数据通过串口转发
 * 参    数：无
 * 返 回 值：无
 */
void cHttpServer::Process(void)
{   	
    this->HTTPServer(); // 定时调用，处理 HTTP 请求
}

/**
 * 函数功能：web服务
 * 参    数：无
 * 返 回 值：无
 */
void cHttpServer::HTTPServer(void)
{

    //uint8_t socknumlist[3] = {0, 1, 2}; 
    for (uint8_t i = 0; i <sizeof(socknumlist) / sizeof(socknumlist[0]) ; i++)
    {
			  //printf("Loop i = %d, socket = %d\n", i, socknumlist[i]);
        //httpServer_run(socknumlist[i]); // HTTP Server handler
			  httpServer_run(i); // HTTP Server handler
    }

    //printf("Calling loopback_tcps()\n");
    
    //调试tcpclient暂时注释 *dyq*这个开启会占用主线程
    //loopback_tcps(0, RX_BUF, 5000);
}